package com.carkeeper.userbill.service.impl;

import com.carkeeper.caruser.feign.UserFeign;
import com.carkeeper.caruser.pojo.User;
import com.carkeeper.subject.feign.StoreFeign;
import com.carkeeper.subject.pojo.StoreFull;
import com.carkeeper.userbill.dao.UserBillLogMapper;
import com.carkeeper.userbill.dao.UserBillMapper;
import com.carkeeper.userbill.pojo.UserBill;
import com.carkeeper.userbill.pojo.UserBillLog;
import com.carkeeper.userbill.service.UserBillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserBillServiceImpl implements UserBillService {

    @Autowired
    private UserBillMapper userBillMapper;

    @Autowired
    private UserBillLogMapper userBillLogMapper;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private StoreFeign storeFeign;

    /**
     * 分页条件查询
     * @param searchMap
     * @return
     */
    @Override
    public PageInfo<UserBill> findByCondition(Map<String,String> searchMap) {
        Example example = new Example(UserBill.class);
        Example.Criteria criteria = example.createCriteria();



        int pageSize = 20;
        int pageNum = 1;

        if (searchMap != null){

            if (searchMap.get("startPage") != null && searchMap.get("pageSize") != null){

                if (!"".equals(searchMap.get("startPage"))){
                    pageNum = Integer.parseInt(searchMap.get("startPage"));
                }

                if (!"".equals(searchMap.get("pageSize"))){
                    pageSize = Integer.parseInt(searchMap.get("pageSize"));
                }
            }

            //订单编码
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))){

                criteria.andEqualTo("code", searchMap.get("code"));
            }
            //客户名称
            if (searchMap.get("contact") != null && !"".equals(searchMap.get("contact"))){

                criteria.andEqualTo("contact", searchMap.get("contact"));
            }
            //支付状态
            if (searchMap.get("payStatus") != null && !"".equals(searchMap.get("payStatus"))){

                criteria.andEqualTo("payStatus", searchMap.get("payStatus"));
            }
            //起始时间
            if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime"))){

                criteria.andGreaterThanOrEqualTo("createTime", searchMap.get("startTime"));
            }
            //终止时间
            if (searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime"))){

                criteria.andLessThanOrEqualTo("createTime", searchMap.get("endTime"));
            }
            //账单类型
            if (searchMap.get("subtypeName") != null && !"".equals(searchMap.get("subtypeName"))){

                criteria.andEqualTo("subtypeName",searchMap.get("subtypeName"));
            }
        }

        PageHelper.startPage(pageNum,pageSize);

        List<UserBill> userBillList = userBillMapper.selectByExample(example);


        PageInfo<UserBill> pageInfo = new PageInfo<>(userBillList);
        return pageInfo;
    }

    /**
     * 支付状态展示列表
     * @param code
     * @return
     */
    @Override
    public Map<String,Object> findByCode(String code) {

        Map<String,Object> resultMap = new HashMap<>();

        if (code==null){
            throw new RuntimeException("账单编号不存在");
        }

        UserBill userBill = userBillMapper.findByCode(code);

        if (userBill==null){
            throw new RuntimeException("账单编号不存在");
        }

        String userId = userBill.getUserId();

        //根据用户id查用户信息
        User user = userFeign.findUserById(userId).getData();
        resultMap.put("username",user.getUsername());//联系人
        resultMap.put("cityCode",user.getCityCode());//城市编码
        resultMap.put("nickname",user.getNickname());//用户名称
        resultMap.put("phone",user.getPhone());//联系电话
        resultMap.put("address",user.getAddress());//地址

        //根据商家id查商家信息
        String orderId = userBill.getOrderId();
        StoreFull storeFull = storeFeign.findById(orderId).getData();
        resultMap.put("store",storeFull.getStore());//商家信息

        //账单数据封装
        resultMap.put("code",userBill.getCode());//账单编号
        resultMap.put("contact",userBill.getContact());
        resultMap.put("price",userBill.getPrice());//账单金额
        resultMap.put("summary",userBill.getSummary());//账单摘要
        resultMap.put("remarks" ,userBill.getRemarks());//备注
        resultMap.put("createTime",userBill.getCreateTime());//生成时间

        //操作记录数据封装
        Integer billId = userBill.getId();
        UserBillLog userBillLog = userBillLogMapper.findByBillId(billId);
        resultMap.put("id", userBillLog.getId());//id
        resultMap.put("handleTime",userBillLog.getHandleTime());//操作时间
        resultMap.put("type",userBillLog.getType());//类型
        resultMap.put("description",userBillLog.getDescription());//描述
        resultMap.put("billStatus",userBillLog.getBillStatus());//支付状态
        resultMap.put("userRealname",userBillLog.getUserRealname());//操作员

        return resultMap;
    }

    /**
     * 支付状态列表展示
     * @return
     */
    @Override
    public List findByPayStatus() {
        List list = userBillMapper.findByPayStatus();
        return list;
    }

    /**
     * 账单类型列表展示
     * @return
     */
    @Override
    public List<String> findBySubtypeName() {
        List list = userBillMapper.findBySubtypeName();
        return list;
    }

}
